package gov.tax.rti.base.jdbc;

import gov.tax.rti.ext.bdf2.jdbc.JdbcDaoSupport;

import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bstek.dorado.core.Configure;

@Repository
public class BaseJdbcDaoSupport<T, PK extends Serializable> extends JdbcDaoSupport<T, PK>{
	
	public static final String BASE_DATASOURCE_NAME = "rti.base.dataSourceName";
	
    protected String getModuleFixDataSourceName()
    {
    	return Configure.getString(BASE_DATASOURCE_NAME);
    }

    protected String getPagingSql(String sql ,int firstEntityIndex,int lastEntityIndex){
		StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
		paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
		paginationSQL.append(sql);
		paginationSQL.append("  ) temp where ROWNUM <= " + lastEntityIndex);
		paginationSQL.append(" ) WHERE  num > " + firstEntityIndex);
		return paginationSQL.toString();
	}
   
    protected int getCount(String sql,Object[] args){
		return this.getJdbcTemplate().queryForInt("select count(1) from ( "+sql+") ", args);
	}
    
    protected int getCount(String sql,Map<String,Object> args){
		return this.getNamedParameterJdbcTemplate().queryForInt("select count(1) from ( "+sql+") ", args);
	}
}
